package com.com.zs21cp.SourceAssemble;


import com.com.zs21cp.ToolBean.WaterSensor;
import com.com.zs21cp.ToolBean.WaterSensorMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.SideOutputDataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;


public class dataSourceTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        OutputTag outputTag1 = new OutputTag<>("s1", Types.POJO(WaterSensor.class));
        OutputTag outputTag2 = new OutputTag<>("s2", Types.POJO(WaterSensor.class));
        SingleOutputStreamOperator<WaterSensor> pr = env
                .socketTextStream("hadoop2", 7777)
                .map(new WaterSensorMapFunction())
                .process(new ProcessFunction<WaterSensor, WaterSensor>() {
                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<WaterSensor> out) throws Exception {
                        if ("s1".equals(value.getId())) {
                            ctx.output(outputTag1, value);

                        } else if ("s2".equals(value.getId())) {
                            ctx.output(outputTag2, value);
                        } else {
                            out.collect(value);
                        }
                    }
                });
        SideOutputDataStream sideOutput = pr.getSideOutput(outputTag1);
        SideOutputDataStream sideOutput1 = pr.getSideOutput(outputTag2);
        sideOutput1.print("s2的测输出流");
        sideOutput.print("s1的测输出流");
        pr.print("主流");

//                .keyBy(WaterSensor:: getId)
//                .reduce(new ReduceFunction<WaterSensor>()
//                {
//
//                    @Override
//                    public WaterSensor reduce(WaterSensor value1, WaterSensor value2) throws Exception {
//                        System.out.println("Demo7_Reduce.reduce");
//
//                        int maxVc = Math.max(value1.getVc(), value2.getVc());
//                        //实现max(vc)的效果  取最大值，其他字段以当前组的第一个为主
//                        //value1.setVc(maxVc);
//                        //实现maxBy(vc)的效果  取当前最大值的所有字段
//                        if (value1.getVc() > value2.getVc()){
//                            value1.setVc(maxVc);
//                            return value1;
//                        }else {
//                            value2.setVc(maxVc);
//                            return value2;
//                        }
//                    }
//                })
//                .print();
        env.execute();
    }
}

